# -*- coding: UTF-8 -*-

from link_camera import SocketServer
from threading import Thread
import gol
from queue import *
import os
from sql_operation import UpdateSql
from config.getConfig import *
import paddlex as pdx
import predict_paddle
#import UI

IPADD = getConfig("server.ini", "server", "serveripadd")
PORT = getConfigInt("server.ini", "server", "serverport")

pic_line = Queue(maxsize=0)
predict_line = Queue(maxsize=0)
inf_line = Queue(maxsize=0)
predict_lock = False
gol._init()
gol.set_value('pic_line', pic_line)
gol.set_value('predict_line', predict_line)
gol.set_value('predict_lock', predict_lock)

# 测试用变量
gol.set_value('predict_time', 0)
gol.set_value('predict_times', 0)

gol.set_value('sql_time', 0)
gol.set_value('sql_times', 0)

# def shift(x, testMode = False):
#     num = round(0.0001518 * pow(x, 3) - 0.01079 * pow(x, 2) + 1.305 * x + 0.3326)
#     if testMode:
#         num = abs(num + random.randint(-10, 10))
#     return num


if __name__ == '__main__':
    # Linux
    # os.system("clear")
    # Windows
    os.system("cls")
    print('---服务端启动---')
    print('---正在读取预测模型---')
    # config = load_model()
    model = pdx.deploy.Predictor(
        getConfig("predict.ini", "predict", "modelpath"),
        getConfigBool("predict.ini", "predict", "use_gpu")
    )
    print('---模型读取完成---')
    server = SocketServer(IPADD, PORT)
    thread_accept = Thread(target=server.AcceptClient, args=())
    thread_predict = Thread(target=predict_paddle.predict, args=(model, ))
    thread_mysql = Thread(target=UpdateSql, args=())
    # thread_ui = Thread(target=UI.ShowInf, args=())
    thread_accept.start()
    thread_predict.start()
    thread_mysql.start()
    # thread_ui.start()
    thread_accept.join()
    thread_predict.join()
    thread_mysql.join()
    # thread_ui.join()
    print(thread_predict.is_alive())






